import 'dart:io';
import 'dart:isolate';
import 'package:excel/excel.dart' as top_excel;
import 'package:excel_utils/pages/cpc_report/bean/china_post_file_bean.dart';
import 'package:excel_utils/pages/cpc_report/bean/china_post_luggage_bean.dart';
import 'package:excel_utils/pages/cpc_report/cpc_file/bean/cpc_file_result_bean.dart';
import 'package:excel_utils/pages/cpc_report/cpc_luggage/bean/cpc_result_bean.dart';
import 'package:excel_utils/pages/cpc_report/bean/fee_item_bean.dart';
import 'package:excel_utils/pages/cpc_report/bean/luggeasy_bean.dart';
import 'package:excel_utils/pages/cpc_report/bean/waybill_managent_bean.dart';

// CPCFile 运单管理提取
cpcFileWaybillManagementExtraction(Map<String, dynamic> extractMap) {
  SendPort sendPort = extractMap["sendPort"]; // 端口
  File mainFile = extractMap["file"]; // 主文件
  String sheetName = extractMap["sheetName"]; // sheet名称  默认是 Main 表名
  top_excel.Excel excel = top_excel.Excel.decodeBytes(mainFile.readAsBytesSync()); // 读取主文件
  var mainSheet = excel.sheets[sheetName];
  if (mainSheet == null) {
    sendPort.send({"code": 1, "msg": "没有找到$sheetName表"});
    return;
  }
  List<WaybillManagementBean> rowData = [];
  for (int i = 1; i < mainSheet.rows.length; i++) {
    var row = mainSheet.rows[i];
    WaybillManagementBean waybillManagementBean = WaybillManagementBean();
    waybillManagementBean.waybillNumber = row[84]?.value.toString();  // cf
    waybillManagementBean.accountName = row[4]?.value.toString();    // e
    waybillManagementBean.accountNo = row[3]?.value.toString();       // d
    waybillManagementBean.orderAddress = row[6]?.value.toString(); //g
    waybillManagementBean.orderKg = row[7]?.value.toString();   // h  kg
    waybillManagementBean.entryDate = row[13]?.value.toString();   // h  kg
    rowData.add(waybillManagementBean);
    sendPort.send({"code": 1, "msg": "解析中...", "progress": i/mainSheet.rows.length});
    // *  运单管理
    //  *  G 》F
    //  *  E >B
    //  *  D>C
    //  *  M>D
    //  *  H>I
    //  *  CF>T  标识

  }
  sendPort.send({"code": 0, "msg": "运单管理数据提取成功", "data": rowData});
}

// CPCFile 中邮文件提取
cpcFileChinaPostFileExtraction(Map<String, dynamic> extractMap) {
  SendPort sendPort = extractMap["sendPort"]; // 端口
  File mainFile = extractMap["file"]; // 主文件
  String sheetName = extractMap["sheetName"]; // sheet名称  默认是 Main 表名
  top_excel.Excel excel = top_excel.Excel.decodeBytes(mainFile.readAsBytesSync()); // 读取主文件
  var mainSheet = excel.sheets[sheetName];
  if (mainSheet == null) {
    sendPort.send({"code": 1, "msg": "没有找到$sheetName表"});
    return;
  }
  List<ChinaPostFileBean> rowData = [];
  for (int i = 1; i < mainSheet.rows.length; i++) {
    var row = mainSheet.rows[i];
    ChinaPostFileBean chinaPostFileBean = ChinaPostFileBean();
    chinaPostFileBean.billedDate = row[0]?.value.toString();
    chinaPostFileBean.amount = row[1]?.value.toString();
    chinaPostFileBean.trackingNo = row[8]?.value.toString();
    chinaPostFileBean.cXCAmount = row[10]?.value.toString();
    chinaPostFileBean.localCurrentAction = row[17]?.value.toString();
    rowData.add(chinaPostFileBean);

    sendPort.send({"code": 1, "msg": "解析中...", "progress": i/mainSheet.rows.length});
  }
  /**
   *  中有文件
   *  A  >E
   *  B  >N
   *  I  标识
   *  K>L
   *  R>G
   */
  sendPort.send({"code": 0, "msg": "中邮文件数据提取成功", "data": rowData});
}

// feeitemlist 提取
cpcFileFeeitemlistExtraction(Map<String, dynamic> extractMap) {
  SendPort sendPort = extractMap["sendPort"]; // 端口
  File mainFile = extractMap["file"]; // 主文件
  String sheetName = extractMap["sheetName"]; // sheet名称  默认是 Main 表名
  top_excel.Excel excel = top_excel.Excel.decodeBytes(mainFile.readAsBytesSync()); // 读取主文件
  var mainSheet = excel.sheets[sheetName];
  if (mainSheet == null) {
    sendPort.send({"code": 1, "msg": "没有找到$sheetName表"});
    return;
  }
  List<FeeItemBean> rowData = [];
  for (int i = 1; i < mainSheet.rows.length; i++) {
    var row = mainSheet.rows[i];
    FeeItemBean chinaPostLuggage = FeeItemBean();
    chinaPostLuggage.kg = row[9]?.value.toString();
    chinaPostLuggage.price = row[10]?.value.toString();
    chinaPostLuggage.trackingNumber = row[3]?.value.toString();
    rowData.add(chinaPostLuggage);
    sendPort.send({"code": 1, "msg": "解析中...", "progress": i/mainSheet.rows.length});
  }

  // *  FI
  // D  标识
  // *  J>J
  // *  K>U

  sendPort.send({"code": 0, "msg": "feeitemlist 数据提取成功", "data": rowData});
}

// 写入数据到excel
cpcFileWriteDataToExcel(Map<String, dynamic> writeMap) async {
  SendPort sendPort = writeMap["sendPort"]; // 端口
  File mainFile = writeMap["writeFile"]; // 主文件
  String sheetName = writeMap["sheetName"]; // sheet名称  默认是 Main 表名
  List<CpcFileResultBean> data = writeMap["cpcResultList"]; // 数据
  top_excel.Excel excel = top_excel.Excel.decodeBytes(mainFile.readAsBytesSync()); // 读取主文件
  var mainSheet = excel.sheets[sheetName];
  if (mainSheet == null) {
    sendPort.send({"code": 1, "msg": "没有找到$sheetName表"});
    return;
  }
  for (int index = 0; index < data.length; index++) {
    List<top_excel.CellValue> row = [];
    row.add(top_excel.TextCellValue(data[index].a.toString()));
    row.add(top_excel.TextCellValue(data[index].b.toString()));
    row.add(top_excel.TextCellValue(data[index].c.toString()));
    row.add(top_excel.TextCellValue(data[index].d.toString()));
    row.add(top_excel.TextCellValue(data[index].e.toString()));
    row.add(top_excel.TextCellValue(data[index].f.toString()));
    row.add(top_excel.TextCellValue(data[index].g.toString()));
    row.add(top_excel.TextCellValue(data[index].h.toString()));
    row.add(top_excel.TextCellValue(data[index].i.toString()));
    row.add(top_excel.TextCellValue(data[index].j.toString()));
    row.add(top_excel.TextCellValue(data[index].k.toString()));
    row.add(top_excel.TextCellValue(data[index].l.toString()));
    row.add(top_excel.TextCellValue(data[index].m.toString()));
    row.add(top_excel.TextCellValue(data[index].n.toString()));
    row.add(top_excel.TextCellValue(data[index].o.toString()));
    row.add(top_excel.TextCellValue(data[index].p.toString()));
    row.add(top_excel.TextCellValue(data[index].q.toString()));
    row.add(top_excel.TextCellValue(data[index].r.toString()));
    row.add(top_excel.TextCellValue(data[index].s.toString()));
    row.add(top_excel.TextCellValue(data[index].t.toString()));
    row.add(top_excel.TextCellValue(data[index].u.toString()));
    mainSheet.appendRow(row);
    await Future.delayed(Duration(milliseconds: 10), () {});
    sendPort.send({"code": 2, "msg": "正在写入数据，请稍后... 当前处理的是第 ${index + 1} 行，一共 ${data.length} 行", "progress": (index + 1) / data.length});
  }

  // 写入之后进行保存到他本身
  final bytes = excel.encode();
  dynamic result = {};
  try {
    mainFile.writeAsBytesSync(bytes!);
    result = {"code": 0, "msg": "写入成功"};
  } catch (e) {
    print(e.toString());
    if (e is PathAccessException) {
      result = {"code": 1, "msg": "文件被占用"};
    }
  }
  sendPort.send({"code": 0, "msg": "写入完成"});
}
